package com.yiip.gmall.manager.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.corba.se.spi.orbutil.threadpool.WorkQueue;
import com.yiip.gmall.beans.*;
import com.yiip.gmall.manager.mapper.*;
import com.yiip.gmall.service.CatalogService;
import org.apache.commons.lang3.StringUtils;
import org.jboss.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    CatalogMapper catelogMapper;

    @Autowired
    Catalog2Mapper catelog2Mapper;

    @Autowired
    Catalog3Mapper catelog3Mapper;

    @Autowired
    AttrInfoMapper attrInfoMapper;

    @Autowired
    AttrValueMapper attrValueMapper;

    @Override
    public List<PmsBaseCatalog1> getCatalog1() {
        return catelogMapper.selectList(null);
    }

    @Override
    public List<PmsBaseCatalog2> getCatalog2(String catelogId) {
        QueryWrapper<PmsBaseCatalog2> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catalog1_id", catelogId);
        return catelog2Mapper.selectList(queryWrapper);
    }

    @Override
    public List<PmsBaseCatalog3> getCatalog3(String catalog2Id) {
        QueryWrapper<PmsBaseCatalog3> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catalog2_id", catalog2Id);
        return catelog3Mapper.selectList(queryWrapper);
    }

    @Override
    public List<PmsBaseAttrInfo> getBaseAttrInfoList(String catalog3Id) {
        QueryWrapper<PmsBaseAttrInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catalog3_id", catalog3Id);
        return attrInfoMapper.selectList(queryWrapper);
    }

    @Override
    public void saveAttrInfo(PmsBaseAttrInfo pmsBaseAttrInfo) {
        //如果id不为空，则为更新，否则为保存
        if (StringUtils.isNotBlank(pmsBaseAttrInfo.getId())) {
            attrInfoMapper.updateById(pmsBaseAttrInfo);
            //明细表数据，先删，再插
            QueryWrapper<PmsBaseAttrValue> wrapper = new QueryWrapper<>();
            wrapper.eq("attr_id", pmsBaseAttrInfo.getId());
            attrValueMapper.delete(wrapper);
            List<PmsBaseAttrValue> pmsBaseAttrValues = pmsBaseAttrInfo.getAttrValueList();
            if (pmsBaseAttrValues.size() > 0) {
                for (PmsBaseAttrValue pmsBaseAttrValue : pmsBaseAttrValues) {
                    pmsBaseAttrValue.setAttrId(pmsBaseAttrInfo.getId());
                    attrValueMapper.insert(pmsBaseAttrValue);
                }
            }

        } else {
            //新增属性值。
            attrInfoMapper.insert(pmsBaseAttrInfo);
            List<PmsBaseAttrValue> pmsBaseAttrValues = pmsBaseAttrInfo.getAttrValueList();
            if (pmsBaseAttrValues.size() > 0) {
                for (PmsBaseAttrValue pmsBaseAttrValue : pmsBaseAttrValues) {
                    pmsBaseAttrValue.setAttrId(pmsBaseAttrInfo.getId());
                    attrValueMapper.insert(pmsBaseAttrValue);
                }
            }

        }

    }

    @Override
    public List<PmsBaseAttrValue> getAttrValueList(String attrId) {
        QueryWrapper<PmsBaseAttrValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attr_id", attrId);
        List<PmsBaseAttrValue> pmsBaseAttrValueList = attrValueMapper.selectList(queryWrapper);
        return pmsBaseAttrValueList;
    }
}
